﻿using System;
using YanZhiwei.DotNet2.Utilities.Base;
namespace YanZhiwei.DotNet2.Utilities.Common
{
    /// <summary>
    /// 检查 帮助类
    /// </summary>
    public static class CheckHelper
    {
        #region 是否是布尔类型
        /// <summary>
        /// 是否是布尔类型
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>布尔类型</returns>
        public static bool IsBool(this object data)
        {
            switch (data.ToString().Trim().ToLower())
            {
                case "0":
                    return false;
                case "1":
                    return true;
                case "是":
                    return true;
                case "否":
                    return false;
                case "yes":
                    return true;
                case "no":
                    return false;
                default:
                    return false;
            }
        }
        #endregion
        #region  判断字符串是否在某个范围
        /// <summary>
        /// 判断字符串是否在某个范围
        /// <para>eg:CheckHelper.InRange("2", 1, 5);==>true</para>
        /// <para>判断：小于等于并且大于等于</para>
        /// </summary>
        /// <param name="data">判断字符串</param>
        /// <param name="minValue">范围最小值</param>
        /// <param name="maxValue">范围最大值</param>
        /// <returns>是否在某个范围</returns>
        public static bool InRange(this string data, int minValue, int maxValue)
        {
            bool _result = false;
            int _number = -1;
            if (int.TryParse(data, out _number))
            {
                _result = (_number >= minValue && _number <= maxValue);
            }
            return _result;
        }
        #endregion
        #region 验证是否是数字
        /// <summary>
        /// 是否是数字
        /// <para>eg:CheckHelper.IsNumber("abc");==>false</para>
        /// </summary>
        /// <param name="data">判断字符串</param>
        /// <returns>是否是数字</returns>
        public static bool IsNumber(this string data)
        {
            return RegexHelper.IsMatch(data, RegexPattern.NumberCheck);
        }
        #endregion
        #region 验证非空
        /// <summary>
        /// 验证非空
        /// </summary>
        /// <param name="data">验证对象</param>
        /// <returns> 验证非空</returns>
        public static bool NotNull(object data)
        {
            return !(data == null);
        }
        #endregion 验证非空
        #region 验证是否是email
        /// <summary>
        /// 验证是否是email
        /// <para>eg:CheckHelper.IsEmail("Yan.Zhiwei@hotmail.com");==true</para>
        /// </summary>
        /// <param name="data">验证字符串</param>
        /// <returns>是否是email</returns>
        public static bool IsEmail(this string data)
        {
            return RegexHelper.IsMatch(data, RegexPattern.EmailCheck);
        }
        #endregion
        #region 验证是否是URL
        /// <summary>
        /// 验证是否是URL
        /// <para>eg:CheckHelper.IsURL("www.cnblogs.com/yan-zhiwei");==>true</para>
        /// </summary>
        /// <param name="data">验证字符串</param>
        /// <returns>是否是URL</returns>
        public static bool IsURL(this string data)
        {
            return RegexHelper.IsMatch(data, RegexPattern.URLCheck);
        }
        #endregion
        #region 验证是否是文件路径
        /// <summary>
        /// 验证是否是文件路径
        /// <para>eg:CheckHelper.IsFilePath(@"C:\alipay\log.txt");==>true</para>
        /// </summary>
        /// <param name="data">验证字符串</param>
        /// <returns>是否是文件路径</returns>
        public static bool IsFilePath(this string data)
        {
            return RegexHelper.IsMatch(data, RegexPattern.FileCheck);
        }
        #endregion
        #region  验证是否是邮政编码
        /// <summary>
        /// 验证是否是邮政编码
        /// </summary>
        /// <param name="data">验证字符串</param>
        /// <returns>是否是邮政编码</returns>
        public static bool IsPoseCode(this string data)
        {
            return RegexHelper.IsMatch(data, RegexPattern.PostCodeCheck);
        }
        #endregion
        #region 验证是否是IP
        /// <summary>
        /// 是否是IP
        /// </summary>
        /// <param name="data">需要检测到IP</param>
        /// <returns>Validation</returns>
        public static bool IsIp(string data)
        {
            return RegexHelper.IsMatch(data, RegexPattern.IpCheck);
        }
        #endregion
        #region 验证是否是整数
        /// <summary>
        /// 是否是整数
        /// </summary>
        /// <param name="validation">Validation</param>
        /// <param name="intString">需要检测的字符串</param>
        /// <param name="argumentName">参数名称</param>
        /// <returns>Validation</returns>
        public static bool IsInt(string data)
        {
            return RegexHelper.IsMatch(data, RegexPattern.IntCheck);
        }
        #endregion
        #region 验证是否是中文
        /// <summary>
        /// 是否是中文
        /// </summary>
        /// <param name="data">验证字符串</param>
        /// <returns>是否是中文</returns>
        public static bool IsChinses(this string data)
        {
            return RegexHelper.IsMatch(data, RegexPattern.IntCheck);
        }
        #endregion
        #region 验证是否是身份证号码
        /// <summary>
        /// 是否是身份证号码
        /// </summary>
        /// <param name="data">验证数据</param>
        /// <returns>是否是身份证号码</returns>
        public static bool IsIdCard(string data)
        {
            return RegexHelper.IsMatch(data, RegexPattern.IdCardCheck);
        }
        #endregion
        #region 验证是否是十六进制字符串
        /// <summary>
        /// 是否是十六进制字符串
        /// </summary>
        /// <param name="data">验证数据</param>
        /// <returns>是否是十六进制字符串</returns>
        public static bool IsHexString(string data)
        {
            return RegexHelper.IsMatch(data, RegexPattern.HexStringCheck);
        }
        #endregion
        #region 验证是否是Base64
        /// <summary>
        /// 是否是Base64
        /// </summary>
        /// <param name="data">验证数据</param>
        /// <returns>是否是Base64</returns>
        public static bool IsBase64(string data)
        {
            return (data.Length % 4) == 0 && RegexHelper.IsMatch(data, RegexPattern.Base64Check);
        }
        #endregion
        #region 验证是否图片byte[]是否合法
        /// <summary>
        /// 判断图片byte[]是否合法 
        /// </summary>
        /// <param name="data">图片byte[]</param>
        /// <returns>是否合法</returns>
        public static bool IsImageFormat(byte[] data)
        {
            if (data == null || data.Length < 4)
            {
                return false;
            }
            string _fileClass = "";
            int _len = data.Length;
            try
            {
                _fileClass = data[0].ToString();
                _fileClass += data[1].ToString();
                _fileClass = _fileClass.Trim();
                if (_fileClass == "7173" || _fileClass == "13780")//7173:gif;13780:PNG;
                {
                    return true;
                }
                else      // Jpg,Jpeg
                {
                    byte[] _jpg = new byte[4];
                    _jpg[0] = 0xff;
                    _jpg[1] = 0xd8;
                    _jpg[2] = 0xff;
                    _jpg[3] = 0xd9;
                    if (data[0] == _jpg[0] && data[1] == _jpg[1]
                        && data[_len - 2] == _jpg[2] && data[_len - 1] == _jpg[3])
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        #endregion
        #region 验证是否是日期格式
        /// <summary>
        /// 是否是日期格式
        /// <para>eg:CheckHelper.IsDate("2014年12月12日");==>true</para>
        /// </summary>
        /// <param name="data">需要判断字符串</param>
        /// <returns>是否是日期格式</returns>
        public static bool IsDate(this string data)
        {
            if (String.IsNullOrEmpty(data)) return false;
            if (RegexHelper.IsMatch(data, RegexPattern.DateCheck))
            {
                data = data.Replace("年", "-");
                data = data.Replace("月", "-");
                data = data.Replace("日", " ");
                data = data.Replace("  ", " ");
                DateTime _date;
                return DateTime.TryParse(data, out _date);
            }
            return false;
        }
        #endregion 
    }
}
